//
//  LCRegistVC.m
//  LongChain
//
//  Created by FengZi on 2018/4/8.
//  Copyright © 2018年 FengZi. All rights reserved.
//

#import "LCRegistVC.h"
#import "LCUserModel.h"

@interface LCRegistVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *registTextField;

@property (weak, nonatomic) IBOutlet UIButton *nextButton;

@property (nonatomic, copy) NSString *registCode;

@end

@implementation LCRegistVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self customTranslucentNavigationBar];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImageView *bgImage = [[UIImageView alloc]initWithFrame:self.view.frame];
    bgImage.image = [UIImage imageNamed:@"publicBackground"];
    [self.view addSubview:bgImage];
    [self.view insertSubview:bgImage atIndex:0];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(registTextFieldChange) name:UITextFieldTextDidChangeNotification object:nil];
    

}

- (void)registTextFieldChange {
    if (self.registTextField.text.length >= 11) {
        NSString *currentPhone = self.registTextField.text;
        self.registTextField.text = [currentPhone substringWithRange:NSMakeRange(0, 11)];
        [self.registTextField resignFirstResponder];
        self.nextButton.userInteractionEnabled = YES;
        self.nextButton.backgroundColor = [UIColor whiteColor];
        UIColor *titleColor = [UIColor colorWithRed:80/255.0 green:149/255.0 blue:244/255.0 alpha:1];
        [_nextButton setTitleColor:titleColor forState:UIControlStateNormal];
        [_nextButton setTitleColor:titleColor forState:UIControlStateSelected];


        
    }else{
        self.nextButton.userInteractionEnabled = NO;
        self.nextButton.backgroundColor = RGBAlpha(255.0f, 255.0f, 255.0f, 0.3);
        [self.nextButton setTitleColor:RGBAlpha(255.0f, 255.0f, 255.0f, 0.6) forState:UIControlStateNormal];
        [self.nextButton setTitleColor:RGBAlpha(255.0f, 255.0f, 255.0f, 0.6) forState:UIControlStateNormal];

        
    }
}



- (IBAction)registShowShowVerificationCode:(id)sender {

    
    if (self.registTextField.text.length == 11) {
        
        NSDictionary *sendRegistCode = @{@"phone":self.self.registTextField.text};
        [[LCNetwork shareInstance]lc_postNetworkWithRUL:@"/api/v0/sms" parameter:sendRegistCode responseSuccess:^(id resultCode, id resultParameter) {
            
            
            LCLog(@"resultCode = %@ resultParameter = %@",resultCode,resultParameter);
            NSString *code = [NSString stringWithFormat:@"%@",resultParameter[@"sms_code"]];
            self.registCode = code;
            NSString *idStr = [NSString stringWithFormat:@"%@",resultParameter[@"id"]];
            NSString *token = [NSString stringWithFormat:@"%@",resultParameter[@"sms_token"]];
            
            LCUserModel *userModel = [LCUserModel userModelManager];
            userModel.userid = token;
            LCLog(@"idStr = %@ token = %@",idStr,token);

            [self performSegueWithIdentifier:@"showVerificationCode" sender:self];

            
        } responseFail:^(id resultCode, NSError *error) {
            
            [self showHint:@"网络异常,请稍后再试"];
        }];
        
    }else{
        [self showHint:@"请输入正确的手机号"];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"showVerificationCode"]) {
        
        id vc = [segue destinationViewController];
        [vc setValue:self.registTextField.text forKey:@"phoneNum"];
        [vc setValue:self.registCode forKey:@"registCode"];
        [vc setValue:@"0" forKey:@"type"];

    }
}


@end
